package com.zzl.bonds.ticket.manager;

import com.zzl.bonds.ticket.entity.TicketEntity;
import com.zzl.bonds.ticket.service.TicketOpenService;
import com.zzl.bonds.ticket.service.TicketService;
import com.zzl.general.common.exception.SysException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author ：hyt
 * @description：我的票据通用处理逻辑
 * @date ：2021/12/8 13:47
 */
@Component
@Slf4j
public class TicketManager {
    @Autowired
    private TicketService ticketService;

    @Autowired
    private TicketOpenService ticketOpenService;

    /**
     * 新增转让申请时处理我的票据信息
     * 扣除我的票据剩余金额，修改转让金额，
     *
     * @param ticketEntity
     * @param transferAmount
     */
    @Transactional(rollbackFor = Exception.class)
    public void remainingAmountAfterDeduction(TicketEntity ticketEntity, Double transferAmount) throws SysException {
        //扣除可用金额
        ticketEntity.setTicketAvailableAmount(sub(ticketEntity.getTicketAvailableAmount(),transferAmount));
        //转让冻结金额
        ticketEntity.setTransferFrozenAmount(add(ticketEntity.getTransferFrozenAmount(), transferAmount));
        if (!ticketService.updateById(ticketEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 转让签收审批通过且票交所成功后处理票
     *
     * @param ticketEntity
     * @param transferAmount
     */
    @Transactional(rollbackFor = Exception.class)
    public void transferSuccessfulProcessingTicket(TicketEntity ticketEntity, Double transferAmount) throws SysException {
        //设置票据剩余金额
        ticketEntity.setTicketSurplusAmount(sub(ticketEntity.getTicketSurplusAmount() ,transferAmount));
        //已转让金额
        ticketEntity.setTransferAmount(add(ticketEntity.getTransferAmount() , transferAmount));
        //转让中冻结金额
        ticketEntity.setTransferFrozenAmount(sub(ticketEntity.getTransferFrozenAmount() ,transferAmount));
        if (!ticketService.updateById(ticketEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 转让审批失败后处理票
     *
     * @param ticketEntity
     * @param transferAmount
     */
    @Transactional(rollbackFor = Exception.class)
    public void transferFailProcessingTicket(TicketEntity ticketEntity, Double transferAmount) throws SysException {
        //票据金额
        ticketEntity.setTicketAvailableAmount(add(ticketEntity.getTicketAvailableAmount(), transferAmount));
        //转让中冻结金额
        ticketEntity.setTransferFrozenAmount(sub(ticketEntity.getTransferFrozenAmount(),transferAmount));
        if (!ticketService.updateById(ticketEntity)) {
            throw new SysException("A00001", "保存失败！");
        }
    }

    public double sub(double v1, double v2) {

        BigDecimal b1 = new BigDecimal(Double.toString(v1));

        BigDecimal b2 = new BigDecimal(Double.toString(v2));

        return b1.subtract(b2).doubleValue();

    }

    /**
     *   * double类型加法
     * <p>
     *   * @param v1
     * <p>
     *   * @param v2
     * <p>
     *   * @return
     * <p>
     *  
     */
    public double add(double v1, double v2) {

        BigDecimal b1 = new BigDecimal(Double.toString(v1));

        BigDecimal b2 = new BigDecimal(Double.toString(v2));

        return b1.add(b2).doubleValue();

    }

}
